package utilities.event;

import hibernate.HibernateInitializer;
import hibernate.billStream.Service;
import hibernate.billStream.ServiceWrapper;
import hibernate.clientAccount.SingleClient;
import hibernate.hibernateInterface.ClientSystemHibernateInterface;
import hibernate.hibernateInterface.RoomSystemHibernateInterface;
import hibernate.clientAccount.PriceCutInfo;
import java.util.*;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.*;

import utilities.utilities_components.PlainList;
import utilities.utilities_components.SumUp;

public class DPListener implements EventListener{
		
	public DPListener(){}
		
	public void onEvent( Event event ){
		Window win = (Window)event.getTarget();
		String eventType = (String)win.getPage().getAttribute("EVENT_TYPE");
		if (eventType.equals("add"))
			((PlainList)event.getPage().getAttribute("list")).update(
																 getData(event.getTarget(),
																		 event.getPage().getAttribute("service").toString(),
																		 (Double)event.getPage().getAttribute("rate1"),
																		 (Double)event.getPage().getAttribute("rate2"),
																		 (Double)event.getPage().getAttribute("rate3"),
																		 (Date)event.getPage().getAttribute("timeFrom"),
																		 (Date)event.getPage().getAttribute("timeTo")
																		 )
																);
		else if(eventType.equals("delete"))
			deleteData(event.getPage().getAttribute("ID").toString(),event.getTarget());
		else if(eventType.equals("edit"))
			((PlainList)event.getPage().getAttribute("list")).update(
																	editData(
																			event.getPage().getAttribute("service").toString(),
																			 (Double)event.getPage().getAttribute("rate1"),
																			 (Double)event.getPage().getAttribute("rate2"),
																			 (Double)event.getPage().getAttribute("rate3"),
																			 (Date)event.getPage().getAttribute("timeFrom"),
																			 (Date)event.getPage().getAttribute("timeTo")
																			 )
																	);
		else flush(event.getTarget());
	}
	
	private List flush(Component comp){
		Session session=null;
		try{
			Combobox box=(Combobox)comp.getPage().getAttribute("INIT_BOX");
		
			session = HibernateInitializer.getSession();
			Transaction trans=session.beginTransaction();
		
			List serviceList = RoomSystemHibernateInterface.getAllServices(session);
			for (int i =0 ;i < serviceList.size();i++)
				box.appendItem(((ServiceWrapper)serviceList.get(i)).getInner().getName());
	
			List discountList=ClientSystemHibernateInterface.getAllDiscountInfo(session);
			List ret=new LinkedList();
			Iterator<PriceCutInfo> itr=discountList.iterator();
			while(itr.hasNext()){
				PriceCutInfo info=itr.next();
				List col=new LinkedList();
				ret.add(col);
				col.add(info.getId());
				col.add(info.getService().getInner().getName());
				col.add(info.getNormalRate());
				col.add(info.getAdvancedRate());
				col.add(info.getMdRate());
				col.add(info.getStart());
				col.add(info.getTimeTo());
			}
			
			comp.getPage().setAttribute("UPDATE_DATA", ret);
			
			trans.commit();
			session.close();
			return ret;
		}catch(Exception exp){
			exp.printStackTrace();
			try{new Messagebox().show("数据库同步失败");}catch(Exception messageExp){}
			return null;
		}finally{
			if(session != null && session.isOpen())
				session.close();
		}
	}
	
	private List getData(Component comp,String serviceName,double rate1,double rate2,double rate3,Date timeFrom,Date timeTo)
	{
		Session session=null;
		try{
			session=HibernateInitializer.getSession();
			Transaction transaction=session.beginTransaction();
			
			ServiceWrapper wrapper=null;
			try{
				Iterator<ServiceWrapper> itr=(Iterator<ServiceWrapper>) 
					RoomSystemHibernateInterface.getAllServices(session).iterator();
				while(itr.hasNext()){
					ServiceWrapper next=itr.next();
					if(next.getInner().getName().equals(serviceName)){
						wrapper=next;
						break;
					}
				}
				if(wrapper == null)
					throw new Exception();
			}catch(Exception e){
				try{new Messagebox().show("未知服务名称");}catch(Exception messageExp){}
				throw new Exception();
			}
			
			PriceCutInfo dInfo=null;
			try{
				dInfo=ClientSystemHibernateInterface.getDiscountInfoByService(wrapper, session);
			}catch(Exception e){}
			
			if(dInfo == null){
				dInfo=new PriceCutInfo();
				
				dInfo.setStart(timeFrom);
				dInfo.setTimeTo(timeTo);
				dInfo.setService(wrapper);
				dInfo.setNormalRate(rate1);
				dInfo.setAdvancedRate(rate2);
				dInfo.setMdRate(rate3);
				
				session.save(dInfo);
				transaction.commit();
				session.close();
			}else{
				dInfo.setStart(timeFrom);
				dInfo.setTimeTo(timeTo);
				dInfo.setService(wrapper);
				dInfo.setNormalRate(rate1);
				dInfo.setAdvancedRate(rate2);
				dInfo.setMdRate(rate3);
				
				session.saveOrUpdate(dInfo);
				session.flush();
				transaction.commit();
				session.close();
			}
				
		}catch(Exception e){
			e.printStackTrace();
			try{new Messagebox().show("记录插入失败");}catch(Exception messageExp){}
		}finally{
			if(session != null && session.isOpen())
				session.close();
		}
		
		return flush(comp);
	}
	
	private void deleteData(String id,Component comp){
		Session session=null;
		try{
			session=HibernateInitializer.getSession();
			Transaction transaction=session.beginTransaction();
			
			PriceCutInfo info=new PriceCutInfo();
			info.setId(new Long(id));
			session.delete(info);
			
			transaction.commit();
			session.close();
			
		}catch(Exception e){
			e.printStackTrace();
			try{new Messagebox().show("记录插入失败");}catch(Exception messageExp){}
		}finally{
			if(session != null && session.isOpen())
				session.close();
		}
		
		flush(comp);
	}
	
	private List editData(String serviceName,double rate1,double rate2,double rate3,Date timeFrom,Date timeTo)
	{
		return null;
	}
	
}